package edu.cmu.cs.stage3.alice.authoringtool.dialog;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.core.Element;
import edu.cmu.cs.stage3.alice.core.UnresolvablePropertyReferencesException;
import edu.cmu.cs.stage3.alice.core.reference.PropertyReference;
import edu.cmu.cs.stage3.io.DirectoryTreeLoader;
import edu.cmu.cs.stage3.progress.ProgressCancelException;
import edu.cmu.cs.stage3.progress.ProgressPane;
import edu.cmu.cs.stage3.swing.DialogManager;

public class LoadElementProgressPane extends ProgressPane {

 private DirectoryTreeLoader m_loader;
 private Element m_externalRoot;
 private Element m_loadedElement;

 public LoadElementProgressPane (String title, String preDescription ) {
  super( title, preDescription );
  // System.out.println("\n\n\n\nLoadElementProgressPane.java: LoadElementProgressPane(...): "+preDescription);
  }

 //Rem @Override
 // protected void construct() throws ProgressCancelException
 //L in order to be accedd by: AuthoringTool.loadWorld(final DirectoryTreeLoader loader,Object path,boolean askForSaveIfNecessary)'
 //L Called by: at edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool.loadWorld(AuthoringTool.java:1174)
 public void construct () throws ProgressCancelException {
  // try{String str1=""; System.out.println(str1.charAt(3));}catch(Exception e){e.printStackTrace(); }
  m_loadedElement = null;
  try {
   m_loadedElement = Element.load( m_loader, m_externalRoot, this ); // edu.cmu.cs.stage3.alice.core.Element.load(...)
  } catch( ProgressCancelException pce ) {
   throw pce;
  } catch( UnresolvablePropertyReferencesException upre ) {
   StringBuffer sb = new StringBuffer();
   sb.append( "WARNING: unable to resolve references: \n" );
   PropertyReference[] propertyReferences = upre.getPropertyReferences();
   //LOB for (PropertyReference propertyReference : propertyReferences)
   PropertyReference [] propertyReference = propertyReferences;
   int _tam = propertyReference!=null ? propertyReference.length : 0;
   for (int _i=0; _i<_tam; _i++) {
    edu.cmu.cs.stage3.alice.core.Property property = propertyReference[_i].getProperty(); //L  propertyReference.getProperty()
    sb.append( "    " );
    sb.append( property.getOwner().toString() );
    sb.append( '[' );
    sb.append( property.getName() );
    sb.append( "] -> " );
    sb.append( propertyReference[_i].getCriterion() ); //L propertyReference.getCriterion()
    sb.append( '\n' );
    }
   sb.append( '\n' );
   sb.append( "Would you like to continue, setting all values to None?" );
   if( DialogManager.showConfirmDialog( sb.toString(), "Unable to load world", javax.swing.JOptionPane.YES_NO_OPTION ) == javax.swing.JOptionPane.YES_OPTION ) {
    m_loadedElement = upre.getElement();
    }
  } catch( Throwable t ) {
   AuthoringTool.getInstance().showErrorDialog( "Unable to load world", t );
   }
  }

 public void setLoader (DirectoryTreeLoader loader ) {
  m_loader = loader;
  }

 public void setExternalRoot (edu.cmu.cs.stage3.alice.core.Element externalRoot ) {
  m_externalRoot = externalRoot;
  }

 public edu.cmu.cs.stage3.alice.core.Element getLoadedElement () {
  return m_loadedElement;
  }

 }
